package com.joinway.framework.jpush.service;

import java.util.Collection;
import java.util.Map;

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

import com.joinway.framework.jpush.constant.MobileConstants;

public class IosPushService extends JPushService {

	public IosPushService(String masterSecret, String appKey){
		super(masterSecret, appKey);
	}

	public String getSupportMobileType(){
		return MobileConstants.OS.IOS;
	}

	@Override
	protected PushResult doBroadcastMessage(String title, Map<String, String> extras, long seconds, boolean production) throws APIConnectionException, APIRequestException {
		PushPayload ppl = PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.all())
				.setNotification(Notification.ios(title, extras))
//				.setMessage(Message.newBuilder().setMsgContent(title).addExtras(extras).build())
				.build();
		
		ppl.resetOptionsTimeToLive(seconds);
		ppl.resetOptionsApnsProduction(production);
		
		return client.sendPush(ppl);
	}

	@Override
	protected PushResult doMassMessage(String title, Map<String, String> extras, Collection<String> aliases, long seconds, boolean production) throws APIConnectionException, APIRequestException {
		PushPayload ppl = PushPayload.newBuilder()
//				.setMessage(Message.newBuilder().setMsgContent(title).addExtras(extras).build())
//				.setNotification(Notification.ios(text, extras))
				.setPlatform(Platform.ios())
				.setAudience(Audience.alias(aliases))
				.setNotification(Notification.ios(title, extras))
				.build();
		
		ppl.resetOptionsTimeToLive(seconds);
		ppl.resetOptionsApnsProduction(production);
		
		return client.sendPush(ppl);
	}

}
